// pages/my/my.js
const app = getApp()
const share = require('../../components/utils.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    myInfo: '',
    showlogo: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setStorageSync('onloadto', 1)
    if (wx.getStorageSync('userInfo') != '') {
      console.log('我在这里调用了getMyPageInfo2')
      this.getMyPageInfo()
    }
  },

  getMyPageInfo() {
    wx.request({
      url: app.globalData.Interfaceurl + 'getMyPageInfo',
      method: "post",
      header: {
        "content-type": "application/x-www-form-urlencoded;charset=UTF-8"
      },
      data: {
        userId: app.globalData.userId,
      },
      success: res => {

        // res.data.myInfo={
        //   allNum: 5,
        //   couponNum: 5,
        //   mobile: "",
        //   waitAppraise: 1,
        //   waitArrival: 1,
        //   waitPay: 0,
        //   waitSend: 1,
        //   wxHeadImage: "https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTLHlVINibaslkwOntwBQDJOxa1x8Gh2ibytF2h1CsOp2FjthWxxmtibVO4DpVvx9EjgzUGVIia6OicIdpg/132",
        //   wxNickName: "chiak々"
        // }


        console.log(res)
        if (res.data.result == 0 && res.statusCode == 200) {
          this.setData({
            myInfo: res.data.myInfo
          })
        }


      }
    })
  },
  gonopay() {
    if (wx.getStorageSync('userInfo') != '') {
      wx.navigateTo({
        url: '/pages/order/order?showtype=2',
      })

    } else {
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }


  },
  gonofh() {
    if (wx.getStorageSync('userInfo') != '') {
      wx.navigateTo({
        url: '/pages/order/order?showtype=3',
      })

    } else {
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }


  },
  gonews() {
    wx.navigateTo({
      url: '/pages/news/news',
    })
  },
  gonosh() {
    if (wx.getStorageSync('userInfo') != '') {
      wx.navigateTo({
        url: '/pages/order/order?showtype=4',
      })

    } else {
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }


  },
  gonopj() {

    if (wx.getStorageSync('userInfo') != '') {
      wx.navigateTo({
        url: '/pages/order/order?showtype=5',
      })

    } else {
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }


  },

  goafterorder() {

    if (wx.getStorageSync('userInfo') != '') {
      wx.navigateTo({
        url: '/pages/afterorder/afterorder',
      })

    } else {
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }

  },
  islogin() {
    var t = this
    if (wx.getStorageSync('userInfo') == '') {
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }
    //  if (wx.getStorageSync('userInfo')=='') {
    //   wx.getUserProfile({
    //     desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
    //     success: (res) => {
    //       console.log(res);
    //       this.setData({
    //         userInfo: res.userInfo,
    //         hasUserInfo: true
    //       })
    //       wx.request({
    //         url: app.globalData.Interfaceurl + 'grantWxInfo',
    //         method: "post",
    //         header: {
    //             "content-type": "application/x-www-form-urlencoded;charset=UTF-8"
    //         },
    //         data: {
    //             userId: app.globalData.userId,
    //             rawData:res.rawData,
    //             signature:res.signature,
    //             encryptedData:res.encryptedData,
    //             iv:res.iv
    //         },
    //         success: res => {
    //             console.log(res)
    //             if (res.data.result==0&&res.statusCode==200) {
    //               wx.setStorageSync('userInfo', t.data.userInfo)
    //               t.getMyPageInfo()
    //             }
    //             //console.log(wx.getStorageSync('userInfo')) 


    //         }
    //   })

    //     },
    //     fail: (res) => {
    //       console.log(res);
    //       // this.setData({
    //       //   userInfo: res.userInfo,
    //       //   hasUserInfo: true
    //       // })
    //     }
    //   })
    //  }
  },
  //退出登录
  outlogin() {

    if (wx.getStorageSync('userInfo') != '') {

      wx.setStorageSync('userInfo', '')
      wx.setStorageSync('mobile', '')


      this.setData({
        myInfo: '',
        showlogo: 0
      })
      wx.showToast({
        title: '您已经退出登录，将无法使用部分功能',
        icon: 'none'
      })
      this.onLoad();
    } else {
      wx.showToast({
        title: '您已经退出登录了~',
        icon: "none"
      })
      // wx.navigateTo({
      //   url: '/pages/login/login',
      // })
    }



  },
  //地址管理
  goaddremg() {
    if (wx.getStorageSync('userInfo') != '') {
      wx.navigateTo({
        url: '/pages/addressmg/addressmg',
      })

    } else {
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }


  },
  //优惠卷
  gocoupon() {

    if (wx.getStorageSync('userInfo') != '') {
      wx.navigateTo({
        url: '/pages/coupon/coupon',
      })

    } else {
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }

  },
  gomycomments() {
    wx.makePhoneCall({
      phoneNumber: '13018516327',
    })
  },
  //联系客服
  handleContact(e) {
    console.log(e.detail.path)
    console.log(e.detail.query)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (wx.getStorageSync('onloadto') == 1) {
      wx.setStorageSync('onloadto', wx.getStorageSync('onloadto') + 1)
    } else {
      if (wx.getStorageSync('userInfo') != '') {
        console.log('我在这里调用了getMyPageInfo1')
        this.getMyPageInfo()
      }
    }

    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 3,
        show: true
      })
    }
    console.log('刷新onshow')
    if (this.data.showlogo == 2) {
      console.log('我在这里调用了getMyPageInfo2')
      this.getMyPageInfo()
    }
  },

  Goorder() {
    console.log("111")

    if (wx.getStorageSync('userInfo') != '') {
      wx.navigateTo({
        url: '/pages/order/order?showtype=1',
      })

    } else {
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }

  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return share.share()
  }
})